package it.unisalento.appsalento.asynctask.framework;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.os.AsyncTask;
import android.os.Build;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
//import com.agm.dto.Utente;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sun.jersey.spi.service.ServiceFinder;

public class NetworkHandler {

	private static NetworkHandler instance;

	public static synchronized NetworkHandler getInstance() {
		if (instance == null) {
			instance = new NetworkHandler();
		}
		return instance;
	}

	@SuppressWarnings("rawtypes")
	private NetworkHandler() {
		ServiceFinder.setIteratorProvider(new AndroidServiceIteratorProvider());
	}

	public <T> void read(final String url, final Class<T> clazz,
			final Callback<T> callback) {
		new GetTask(url, new Callback<String>() {

			@Override
			public void callback(String result) {
				// utenti = mapper.readValue(is, Utente[].class);
				ObjectMapper mapper = new ObjectMapper();
				try {
					callback.callback(mapper.readValue(result, clazz));
				} catch (JsonParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (JsonMappingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
//				callback.callback(new GsonBuilder().create().fromJson(result,
//						clazz));
			}
		}).execute();
	}

	public <T> void readList(final String url, final Class<T[]> clazz,
			final Callback<List<T>> callback) {
		new GetTask(url, new Callback<String>() {

			@Override
			public void callback(String result) {
				final T[] array = new GsonBuilder().create().fromJson(result,
						clazz);
				callback.callback(new ArrayList<T>(Arrays.asList(array)));
			}
		}).execute();
	}

	public <T> void write(final String url, final Class<T> clazz, final T t,
			final Callback<T> callback) {
		final Gson gson = new GsonBuilder().create();
		if (Build.VERSION.SDK_INT >= 11) {
			new PostTask(url, gson.toJson(t), new Callback<String>() {

				@Override
				public void callback(String result) {
					// utenti = mapper.readValue(is, Utente[].class);
					ObjectMapper mapper = new ObjectMapper();
					try {
						callback.callback(mapper.readValue(result, clazz));
					} catch (JsonParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (JsonMappingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					// callback.callback(gson.fromJson(result, clazz));
				}
			}).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

		} else {
			new PostTask(url, gson.toJson(t), new Callback<String>() {

				@Override
				public void callback(String result) {
					callback.callback(gson.fromJson(result, clazz));
				}
			}).execute();
		}
	}
}
